import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {TipService} from "../../main/services/tip.service";
import Common from '../../common/common';
import {ExtendHttpService} from "../../common/extend-http.service";
@Component({
  selector: 'app-get-back-pwd2',
  templateUrl: './get-back-pwd2.component.html',
  styleUrls: ['./get-back-pwd2.component.css']
})
export class GetBackPwd2Component implements OnInit {
  isLoading = false;
  tipType = 'error';
  info = {mobile: '', code: '', newPwd: '', oldNew: ''};

  constructor(private activatedRoute: ActivatedRoute,
              private tipService: TipService,
              private extendHttp: ExtendHttpService,
              private router: Router) {
    this.activatedRoute.queryParams.subscribe(res => {
      this.info.mobile = res['mobile'];
      this.info.code = res['code'];
    })
  }

  ngOnInit() {

  }

  onSubmit() {
    if (!this.isLoading) {
      if (!this.info.newPwd.trim()) {
        this.tipService.change(true, '请输入密码');
        return;
      }
      if (!this.info.oldNew.trim()) {
        this.tipService.change(true, '请再次输入新密码');
        return;
      }
      if (this.info.newPwd.trim() != this.info.oldNew.trim()) {
        this.tipService.change(true, '两次密码输入不一致');
        return;
      }
      this.info.newPwd = hex_md5(this.info.newPwd);

      this.isLoading = !this.isLoading;
      this.extendHttp.post('/merchantApp/login/updatePwd', this.info).subscribe(response => {
        let res = response.json();
        if (res.code == '0') {
          this.tipType = 'success';
          this.isLoading = !this.isLoading;
          this.tipService.change(true, res['message']);
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 1500);
        } else {
          this.tipService.change(true, res['message']);
          this.isLoading = !this.isLoading;
        }
      }, error => {
        this.tipService.change(true, '系统繁忙，操作失败！');
        this.isLoading = !this.isLoading;
      });
    }
  }
}
